package com.example.shopping2.a_c_fragment.home

import BaseFragment
import IVIew
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.opengl.Visibility
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.viewpager.widget.ViewPager
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.model.models.HomeModel
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_fragment.home.adapter.*
import com.example.shopping2.a_c_fragment.home.entity.*
import com.example.shopping2.a_c_fragment.home.homevalue.ValueActivity
import com.example.shopping2.a_c_fragment.home.seckill.SeckillActivity
import com.example.shopping2.a_c_fragment.main.LoginActivity
import com.example.shopping2.a_c_network.HttpCode
import com.example.shopping2.a_c_network.LjlUtils
import com.youth.banner.Banner


class HomeFragment : BaseFragment<HomePresenter>(), HomeContract.HomeView, IVIew {
    private val homeSearchbox: SearchBox by lazy { mview.findViewById<SearchBox>(R.id.home_searchbox) }
    private val homeRec: RecyclerView by lazy { mview.findViewById<RecyclerView>(R.id.home_rec) }
    private val homeMenuRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.home_menu_rec) }
    private val homeHotRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.home_hot_rec) }
    private val homevip: ViewPager by lazy { findViewById<ViewPager>(R.id.home_vip) }
    private val mainDialog: LinearLayout by lazy { findViewById<LinearLayout>(R.id.main_dialog) }
    var mybro = MyBro()
    var inflate:View? = null
    private val homeNoticeBanner: Banner<Notice_Entity, Notice_Adapter> by lazy {
        findViewById<Banner<Notice_Entity, Notice_Adapter>>(
            R.id.home_notice_banner
        )
    }
    private val homeBanner: Banner<Recommend_Entity, Recommend_Adapter> by lazy {
        findViewById<Banner<Recommend_Entity, Recommend_Adapter>>(
            R.id.home_banner
        )
    }


    private val menuIcons = intArrayOf(
        R.mipmap.hejiu,
        R.mipmap.haibian,
        R.mipmap.huabing,
        R.mipmap.jiushengyi,
        R.mipmap.haibian,
        R.mipmap.jiushengyi,
        R.mipmap.huabing,
        R.mipmap.hejiu
    )
    private var menutitle = arrayOf("擦,大哥", "窝嫩叠", "才来呢，上炕", "我乃导师田斌", "武喝", "郑州加油", "文喝", "南京加油")
    private var imgbanner = arrayListOf<String>(
        "https://i01piccdn.sogoucdn.com/082fb9a40bab4521",
        "https://i01piccdn.sogoucdn.com/0248ef0ef1d0faf9",
        "https://i02piccdn.sogoucdn.com/367da58fe13055a5",
        "https://i02piccdn.sogoucdn.com/0de8dcfc26ef408f"
    )
    private var textBanner = arrayListOf<String>(
        "吴亦凡说：他很大",
        "吴亦凡说：你忍一下",
        "都美竹说：这世界怎么了"
    )
    private var recom: ArrayList<Recommend_Entity> = ArrayList()
    private var noticelist: ArrayList<Notice_Entity> = ArrayList()
    private var menu: ArrayList<HomeMenuEntity> = ArrayList()
    private var list: ArrayList<HashMap<String, Any>> = ArrayList()
    private var viplist: ArrayList<Vip_Entity> = ArrayList()

    private var adapter: HomeAdaoter? = null
    override fun findlayout(): Int {
        return R.layout.fragment_home
    }

    override fun initView() {
         inflate = LinearLayout.inflate(activity, R.layout.login_dialog, null)

        var boolean = activity!!.getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE).getBoolean("log", false)
        if (boolean){
            inflate?.visibility = View.GONE
        }else{

            var button = inflate?.findViewById<Button>(R.id.login_log_bu)
            button?.setOnClickListener {
                activity!!.startActivity(Intent(activity, LoginActivity::class.java))
            }

            mainDialog.addView(inflate)
        }
        //-----------------------------------------注册广播-------------------------------------
        var intentfilter = IntentFilter()
        intentfilter.addAction("user_login")
        activity?.registerReceiver(mybro,intentfilter)

        createImgBanner()
        createMeun()
        createTextBanner()
        createHot()
        createVip()


    }
    //-----------------------------------------广播接收者-------------------------------------
    inner class MyBro : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == "user_login"){
                var booleanExtra = intent.getBooleanExtra("login_dialog", false)
                if (booleanExtra){
                    inflate?.visibility = View.GONE
                }else{

                    inflate?.visibility = View.VISIBLE


                }

            }
        }

    }

    //-----------------------------------------VIP-------------------------------------
    private fun createVip() {
        for (i in 0..imgbanner.size - 1) {
            viplist.add(Vip_Entity(imgbanner[i]))
        }
        var vipAdapter = Vip_Adapter(viplist)
        homevip.offscreenPageLimit = 3
        homevip.setPageTransformer(true, object : ViewPager.PageTransformer {
            override fun transformPage(page: View, position: Float) {

                //上一页
                if (position < 0) {
                    var scall = 1 + position //当前缩放倍数
                    if (scall < 0.7f) {
                        scall = 0.7f
                    }
                    page.scaleY = scall
                    page.scaleX = scall

                    //当前页
                } else if (position >= 0 && position < 1) {
                    if (position == 0f) {
                        page.scaleY = 1f
                        page.scaleX = 1f
                    } else {
                        var scall = (1 - position) + 0.7f
                        if (scall > 1f) {
                            scall = 1f
                        }
                        page.scaleY = scall
                        page.scaleX = scall
                    }
                    //下一页
                } else {
                    page.scaleY = 0.7f
                    page.scaleX = 0.7f
                }

            }

        })

        homevip.adapter = vipAdapter
    }

    //-----------------------------------------HotData-------------------------------------
    private fun createHot() {
        for (i in 0..menuIcons.size - 1) {
            menu.add(HomeMenuEntity(menuIcons[i], menutitle[i]))
        }
        var hotAdapter = Hot_Adapter(menu)
        homeHotRec.adapter = hotAdapter
        homeHotRec.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true)
    }

    //-----------------------------------------MoticeBanner-------------------------------------
    private fun createTextBanner() {
        for (i in 0..textBanner.size - 1) {
            noticelist.add(Notice_Entity(textBanner[i]))
        }
        var noticeAdapter = Notice_Adapter(noticelist)
        homeNoticeBanner.adapter = noticeAdapter
        homeNoticeBanner.setOrientation(Banner.VERTICAL)
        homeNoticeBanner.start()
    }

    //-----------------------------------------ImageBanner-------------------------------------
    private fun createImgBanner() {
        for (i in 0..imgbanner.size - 1) {
            recom.add(Recommend_Entity(imgbanner[i]))
        }
        var recommendAdapter = Recommend_Adapter(recom, context!!)
        homeBanner.adapter = recommendAdapter
        homeNoticeBanner.start()


    }

    //-----------------------------------------menu-------------------------------------
    private fun createMeun() {
        for (i in 0..menuIcons.size - 1) {
            menu.add(HomeMenuEntity(menuIcons[i], menutitle[i]))
        }
        var menuAdapter = HomeMenuAdapter(menu)
        homeMenuRec.adapter = menuAdapter
        homeMenuRec.layoutManager =
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL)
        menuAdapter.setOnItemClickListener(OnItemClickListener { adapter, view, position ->
            when (position) {
                0 -> {
                    activity?.startActivity(Intent(activity,SeckillActivity::class.java))
                }
                1 -> {

                }
                2 -> {

                }
                3 -> {

                }
                4 -> {

                }
                5 -> {

                }
                6 -> {

                }
                7 -> {

                }
            }
        })

    }

    override fun initData() {
        homeSearchbox.setOnClickListener {
            ToastUtils.showShort("跳转到搜索页")
        }
        mypre = HomePresenter(HomeModel(), this)

        var map = HashMap<String, Any>()
        map.put("code", HttpCode.HOMEDATA)
        map.put("categoryId", "14")
        map.put("pageNo", 1)
        list.add(map)
        mypre.requestAll(list)


    }


    override fun showHomeData(baseEntity: BaseEntity) {
        if (baseEntity is HomeGoodsEntity) {
            var data = baseEntity.data
            adapter = HomeAdaoter(data as ArrayList<HomeGoodsEntity.DataBean>)
            homeRec.adapter = adapter
            homeRec.layoutManager =
                StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            adapter?.setOnItemClickListener { adapter, view, position ->
                var intent = Intent(activity,ValueActivity::class.java)

                intent.putExtra("goods",data[position])

                activity?.startActivity(intent)
            }

        }
    }


}